

#import "YearlyLineChartViewController.h"
#import "BIDAppDelegate.h"

@implementation YearlyLineChartViewController

- (id)init
{
	self = [super init];
	if (self)
	{
		[self.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
		[self setTitle:@"Line Chart"];
		
		_lineChartView = [[PCLineChartView alloc] initWithFrame:CGRectMake(10,10,[self.view bounds].size.width-20,[self.view bounds].size.height-20)];
		[_lineChartView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
		_lineChartView.minValue = 0;
		_lineChartView.maxValue = 200;
        _lineChartView.interval = 20;
        _lineChartView.type = 1;
		[self.view addSubview:_lineChartView];
		
        BIDAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"YearlyData"];
        NSError *error;
        NSArray *objects = [context executeFetchRequest:request error:&error];
        if (objects == nil){
            NSLog(@"Monthly Data in local storage is empty !");
            return nil;
        }
        //NSLog(@"%d", [objects count]);
        NSMutableDictionary *sampleInfo = [[NSMutableDictionary alloc] initWithCapacity:5];
        NSMutableArray *months = [[NSMutableArray alloc] initWithCapacity:12];
        for (int i = 1; i <= 12; i++){
            /*
            if ((i % 3) == 0){
                [days addObject:[NSString stringWithFormat:@"%d",i]];
            }else{
                [days addObject:@""];
            }
             */
            [months addObject:[NSString stringWithFormat:@"%d",i]];
        }
        for (NSManagedObject *object in objects){
            NSString *name = [[NSString alloc] initWithString:[object valueForKey:@"name"]];
            NSMutableArray *lineCode = [[NSMutableArray alloc] initWithCapacity:12];
            for (int i = 1; i <= 12; i++){
                //if ((i % 3) == 0){
                NSString *month;
                if (i < 10){
                    month = [[NSString alloc] initWithFormat:@"m0%d", i];
                }else{
                    month = [[NSString alloc] initWithFormat:@"m%d", i];
                }
                [lineCode addObject:[object valueForKey:month]];
                //}
            }
            [sampleInfo setObject:lineCode forKey:name];
        }
		NSMutableArray *components = [NSMutableArray array];
        int i =0;
		for (NSString *key in [sampleInfo allKeys])
		{
			PCLineChartViewComponent *component = [[PCLineChartViewComponent alloc] init];
			[component setTitle:key];//NSString
			[component setPoints:[sampleInfo objectForKey:key]];//NSArray
			[component setShouldLabelValues:NO];
			
			if (i==0)
			{
				[component setColour:PCColorYellow];
			}
			else if (i==1)
			{
				[component setColour:PCColorGreen];
			}
			else if (i==2)
			{
				[component setColour:PCColorOrange];
			}
			else if (i==3)
			{
				[component setColour:PCColorRed];
			}
			else if (i==4)
			{
				[component setColour:PCColorBlue];
			}
			i++;
			[components addObject:component];
		}
		[_lineChartView setComponents:components];
		[_lineChartView setXLabels:months];
	}
	return self;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	[self.lineChartView setNeedsDisplay];
    return YES;
}

@end
